package com.zll.dms.controller;

import com.zll.dms.exception.MyBaseException;
import com.zll.dms.model.Doc;
import com.zll.dms.pojo.ResultData;
import com.zll.dms.service.DocService;
import com.zll.dms.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 文档相关接口
 */
@Slf4j
@RestController
@RequestMapping("doc")
public class DocController {
    @Autowired
    private DocService docService;

    /**
     * 上传文档
     *
     * @param file
     * @return
     */
    @PostMapping("upload")
    public ResultData uploadDoc(MultipartFile file, HttpServletRequest request) {
        String url = QiniuUtils.uploadFile(file);
        return ResultData.success((Object) url);
    }

    /**
     * 新增doc
     * @return
     */
    @PostMapping("add")
    public ResultData addDoc(Doc doc) {
        docService.addDoc(doc);
        return ResultData.success("添加成功");
    }

    /**
     * 根据筛选条件获取当前用户doc列表
     * @param keyWord
     * @return
     */
    @GetMapping("docs/current")
    public ResultData getCurrentUserDocList(@RequestParam(required = false) String keyWord, long limitNum) {
        return ResultData.success(docService.getCurrentUserDocList(keyWord, limitNum), "操作成功");
    }

    /**
     * 修改文档
     * @param doc
     * @return
     */
    @PostMapping("update")
    public ResultData updateDoc(Doc doc) {
        //验证参数
        if (doc == null || doc.getId() == null) {
            throw new MyBaseException("修改数据有误");
        }
        docService.update(doc);
        return ResultData.success("修改成功");
    }

    /**
     * 删除文档
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultData delete(@PathVariable Long id) {
        if (id == null) {
            return ResultData.error("该文档不存在");
        }
        docService.deleteById(id);
        return ResultData.success("删除成功");
    }

    /**
     * 根据筛选条件获取全部用户doc列表
     * @param keyWord
     * @return
     */
    @GetMapping("docs/all")
    public ResultData getAllUserDocList(@RequestParam(required = false) String keyWord,
                                        @RequestParam(required = false) String userName,long limitNum) {
        return ResultData.success(docService.getAllUserDocList(keyWord,userName, limitNum), "操作成功");
    }


}
